<template>
	<view class="u-demo">
		<view v-if="token==null || token==undefined">
			<br /> <br /> <br />
			<u-row justify="center" gutter="16">
				<u-icon name="calendar-fill" color="#2979ff" size="55"></u-icon>
				<h2>教师督导</h2>
			</u-row>
			<br /> <br />
			<u-form ref="uForm">
				<u-form-item label="账号">
					<u-input placeholder="请输入账号" v-model="userName" />
				</u-form-item>
				<u-form-item label="密码">
					<u-input type="password" placeholder="请输入密码" v-model="password" />
				</u-form-item>
			</u-form>
			<br />
			<u-button type="primary" :loading="loading" @click="getinfo">登录</u-button>
		</view>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userName: "669554",
				password: "123456",
				loading: false
			}
		},
		methods: {
			getinfo() {
				this.loading = true
				let data = {
					username: this.userName,
					password: this.password,
					grant_type: "password",
					scope: "select",
					client_id: "client",
					client_secret: "client",
				}
				this.$u.post('/auth/oauth/token', data).then(res => {
					if (res.data) {
						this.loading = false
						this.$store.commit("setToken", res.data.token)
						this.$store.commit("setUser", res.data.user)
						this.$store.commit("startSocket", res.data.user.userId)
						this.$refs.uToast.show({
							title: '登录成功',
							type: 'success',
							duration: 550,
							url: "/pages/index/index"
						})
					}
				}).catch(c => {
					this.loading = false
					this.$refs.uToast.show({
						title: '登录失败',
						type: 'error',
					})
				})
			},
		},
		onShow() {
			if (this.token != null || this.token != undefined) {
				this.$u.route("pages/index/index")
			}
		},
		onUnload() {
			console.log("token")
		},
		computed: {
			token() {
				return this.$store.state.token
			}
		}
	}
</script>

<style lang="scss" scoped>
	.box {
		padding: 30rpx;
	}

	.box /deep/ button {
		margin-bottom: 40rpx;
	}
</style>
